/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import database.AccesoDB;
import entity.Pais;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Alumno
 */
public class PaisDao implements ICrud<Pais> {

    @Override
    public int grabar(Pais o) throws Exception {
//        throw new UnsupportedOperationException("Not supported yet.");
        Connection cn = null;
        int iResult;
        try {
            cn = AccesoDB.getConnection();
            String query = "insert into countries(country_id,country_name,region_id) values(?,?,?)";
            PreparedStatement pstm = cn.prepareStatement(query);
            pstm.setString(1, o.getIdPais());
            pstm.setString(2, o.getNombrePais());
            pstm.setInt(3, o.getIdRegion());
            iResult = pstm.executeUpdate();
            pstm.close();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return iResult;
    }

    @Override
    public int modificar(Pais o) throws Exception {
//        throw new UnsupportedOperationException("Not supported yet.");
        Connection cn = null;
        int iResult;
        try {
            cn = AccesoDB.getConnection();
            String query = "update countries set country_name=?,region_id=? where country_id=?";
            // country_id es clave, no se modifica
            PreparedStatement pstm = cn.prepareStatement(query);
            pstm.setString(3, o.getIdPais());
            pstm.setString(1, o.getNombrePais());
            pstm.setInt(2, o.getIdRegion());
            iResult = pstm.executeUpdate();
            pstm.close();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return iResult;
    }

    @Override
    public int eliminar(Pais o) throws Exception {
//        throw new UnsupportedOperationException("Not supported yet.");
        Connection cn = null;
        int iResult;
        try {
            cn = AccesoDB.getConnection();
            String query = "delete from countries where country_id=?";
            // country_id es clave, no se modifica
            PreparedStatement pstm = cn.prepareStatement(query);
            pstm.setString(1, o.getIdPais());
            iResult = pstm.executeUpdate();
            pstm.close();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return iResult;
    }

    @Override
    public Pais buscar(Pais o) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Pais> listar() throws Exception {
//        throw new UnsupportedOperationException("Not supported yet.");
        Connection cn = null;
        List<Pais> lista = new ArrayList<Pais>();
        try {
            cn = AccesoDB.getConnection();
            String query = "select * from countries";
            Statement stm = cn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            while (rs.next()) {
                Pais r = new Pais();
                r.setIdPais(rs.getString("country_id"));
                r.setNombrePais(rs.getString("country_name"));
                r.setIdRegion(rs.getInt("region_id"));
                lista.add(r);
            }
            rs.close();
            stm.close();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return lista;
    }

    @Override
    public List<Pais> listarConCriterio(Pais o) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
